define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var pid = $('#pid').val();
    var course_id = $('#course_id').val();
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'material/category/index' + location.search,
                    add_url: 'material/category/add/pid/' + pid + '/course_id/' + course_id,
                    edit_url: 'material/category/edit',
                    del_url: 'material/category/del',
                    multi_url: 'material/category/multi',
                    import_url: 'material/category/import',
                    table: 'material_category',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams: function (params) {
                    params.pid = pid;
                    params.course_id = course_id;
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE',
                        },
                        {
                            field: 'create_time',
                            title: __('Create_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'update_time',
                            title: __('Update_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: function (a, b) {
                                        return `管理${a.name}分类的文件`;
                                    },
                                    title: function (a, b) {
                                        return `管理${a.name}分类的文件`;
                                    },
                                    icon: 'fa fa-align-justify',
                                    classname: 'btn btn-xs btn-primary btn-addtabs',
                                    url: function (a) {
                                        return `material/file/index/category_id/${a.id}`;
                                    },
                                    success: function () {
                                        $(".btn-refresh").trigger("click");
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: '创建子分类',
                                    title: '创建子分类',
                                    icon: 'fa fa-plus',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (a) {
                                        return 'material/category/add/pid/' + a.id + '/course_id/' + course_id;
                                    },
                                    success: function () {
                                        $(".btn-refresh").trigger("click");
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: '打开分类',
                                    title: '打开分类',
                                    icon: 'fa fa-folder-open-o',
                                    classname: 'btn btn-xs btn-primary',
                                    url: function (a) {
                                        return 'material/category/index/pid/' + a.id + '/course_id/' + course_id;
                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    }
                                },
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
